package www.way2inbox.settings;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings.Secure;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author Swetha Bejugam
 * 
 */
public class DeviceDetailsClass {
	private Context con;
	String MailText;

	/* Context */
	public DeviceDetailsClass(Context context) {
		con = context;
	}

	public String getDeviceDetails() {
		/* Get Screen Size in pixels */
		DisplayMetrics metrics = con.getResources().getDisplayMetrics();
		int width = metrics.widthPixels;
		int height = metrics.heightPixels;
		/* Get DeviceDetails */
		String deviceName = getDeviceName();
		String androidVersion = getAndroidVersion();
		String deviceId = getDeviceId();
		/* Getpackagename */
		String packageName = con.getPackageName();
		Intent launchIntent = con.getPackageManager()
				.getLaunchIntentForPackage(packageName);
		/* get Class name */
		String className = launchIntent.getComponent().getClassName();
		MailText = "DeviceName: " + deviceName + "\nAndroidVersion: "
				+ androidVersion + "\nDeviceId: " + deviceId + "\nScreensize: "
				+ width + "X" + height + "Pixels" + "\nPackage Name "
				+ packageName + "\nClass Name: " + className + "\n\n";
		return MailText;
	}

	public String getDeviceName() {

		String manufacturer = Build.MANUFACTURER;
		String model = Build.MODEL;

		if (model.startsWith(manufacturer)) {
			return capitalize(model);
		} else {
			return capitalize(manufacturer) + " " + model;
		}
	}

	private String getAndroidVersion() {
		return Build.VERSION.RELEASE;
	}

	private String capitalize(String s) {
		if (s == null || s.length() == 0) {
			return "";
		}
		char first = s.charAt(0);
		if (Character.isUpperCase(first)) {
			return s;
		} else {
			return Character.toUpperCase(first) + s.substring(1);
		}
	}

	private String getDeviceId() {
		String deviceId = "";
		final TelephonyManager mTelephony = (TelephonyManager) con
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (mTelephony.getDeviceId() != null) {
			deviceId = mTelephony.getDeviceId();
		} else {
			deviceId = Secure.getString(con.getContentResolver(),
					Secure.ANDROID_ID);
		}
		return deviceId;
	}

	public void sendEmail(String MailContent) {
		try {
			if (this.isOnline()) {
				Log.i("Send email", "");
				String content = this.getDeviceDetails() + "\nCrash Message: "
						+ MailContent;
				String[] TO = { "swetha.bejugam@way2online.net" };
				// String[] CC = { "mcmohd@gmail.com" };
				Intent emailIntent = new Intent(Intent.ACTION_SEND);
				emailIntent.setData(Uri.parse("mailto:"));
				emailIntent.setType("text/plain");
				emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
				// emailIntent.putExtra(Intent.EXTRA_CC, CC);
				emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report");
				emailIntent.putExtra(Intent.EXTRA_TEXT, content);
				try {
					// need this to prompts email client only
					emailIntent.setType("message/rfc822");
					con.startActivity(Intent.createChooser(emailIntent,
							"Send mail..."));
					// finish();
					Log.i("Finished sending email...", "");
				} catch (android.content.ActivityNotFoundException ex) {
					Toast.makeText(con, "There is no email client installed.",
							Toast.LENGTH_SHORT).show();
				}
			} else {
			}
		} catch (Exception e) {

		}
	}

	// }

	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) con
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		return netInfo != null && netInfo.isConnectedOrConnecting();
	}
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public boolean getdefaultpackage(String myPackageName){


        if (!Telephony.Sms.getDefaultSmsPackage(con).equals(myPackageName))
        {
            return false;
        }else{
            return true;
        }
    }
    public boolean getversion(){


        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= Build.VERSION_CODES.KITKAT){
            return true;

        }else{

        }return false;
    }
}